import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '_views/LayOut'
import { isLogin } from '_utils/user'
const routes = [
  {
    path: '/',
    name: 'LayOut',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/dashBoard'
      },
      {
        path: '/dashBoard',
        name: '仪表盘',
        component: () => import('_views/DashBoard')
      },
      {
        path: '/itemLists',
        name: '商品列表',
        component: () => import('_views/ItemLists')
      },
      {
        path: '/itemAdd',
        name: '增加商品',
        component: () => import('_views/ItemAdd')
      },
      {
        path: '/itemUpdate',
        name: '修改商品',
        component: () => import('_views/ItemUpdate')
      },
      {
        path: '/cateLists',
        name: '商品分类列表',
        component: () => import('_views/CateLists')
      },
      {
        path: '/userInfo',
        name: '个人中心',
        component: () => import('_views/UserInfo')
      },
      {
        path: '/settings',
        name: '设置',
        component: () => import('_views/Settings')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('_views/LoginPage')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 路由鉴权
router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    // 鉴权
    if (isLogin()) {
      // 登录
      next()
    } else {
      // 未登录
      next('/login')
    }
  } else {
    next()
  }
})

export default router
